package com.zhentao.controller;

import com.github.pagehelper.PageInfo;
import com.zhentao.model.dto.product.ProductDto;
import com.zhentao.model.entity.product.Product;
import com.zhentao.model.vo.common.Result;
import com.zhentao.model.vo.common.ResultCodeEnum;
import com.zhentao.service.ProductService;
import com.zhentao.service.ProductSpecService;
import com.zhentao.service.ProductUnitService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/product/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductUnitService productUnitService;

    @Autowired
    private ProductSpecService productSpecService;

    //分页查询数据
    @GetMapping("/{page}/{limit}")
    public Result<PageInfo<Product>> findByPage(@PathVariable int page,
                                                @PathVariable int limit,
                                                ProductDto productDto){
        PageInfo<Product> byPage = productService.findByPage(page, limit, productDto);
        return Result.build(byPage , ResultCodeEnum.SUCCESS) ;
    }

    //查询所有商品单位
    @GetMapping("/productUnitList")
    public Result productUnitList(){
        return productService.productUnit();
    }

    //添加商品
    @PostMapping(value = "portductAdd")
    public Result productAdd(@RequestBody Product product){
        return productService.productSave(product);
    }


    //查看商品详情
    @GetMapping(value = "/productFindOne/{id}")
    public Result productFindOne(@PathVariable Long id){
        System.err.println(id );
        return productService.productFindOne(id);
    }

    //修改商品
    @PutMapping(value = "/productUpdate")
    public Result productUpdate(@RequestBody Product product){
        return productService.productUpdate(product);
    }

    //删除商品
    @DeleteMapping(value = "/productDelete/{id}")
    public Result productDelete(@PathVariable Long id){
        return productService.productDelete(id);
    }


    //商品审核
    @GetMapping(value = "/updateAuditStatus/{id}/{auditStatus}")
    public Result updateAuditStatus(@PathVariable Long id, @PathVariable Integer auditStatus){
        productService.productExamine(id, auditStatus);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    //商品上下架操作
    @GetMapping(value = "/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        productService.productStatus(id, status);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    //添加图片
    @PostMapping(value = "/file")
    public Result file(MultipartFile file){
        Result file1 = productService.file(file);
        return file1;
    }

}
